package com.algorithm.list;

import java.util.Arrays;

public class ArrayList<T> extends AbstractList<T> {

	final int defaultCapacity  = 10;

	int capacity = 0;

	int insertSize = -1;

	private Object[] t = {};

	public ArrayList(int capacity){
		if(capacity>0){
			this.capacity = capacity;
			t = new Object[capacity];
		}else{
			t = new Object[defaultCapacity];
			this.capacity = defaultCapacity;
		}

	}

	@Override
	public void insert(T o){
		insertSize++;
		rangeCheckForAdd(insertSize);
		t[insertSize] = o;
	}

	@Override
	T get(int position) {
		if(t!=null){
			return (T) t[position];
		}
		return null;
	}

	@Override
	public void delete(T o) {
		//删除的时间复杂度是O（n）的
		//删除一项以后所有后面的位置前移
	}

	@Override
	void remove(int position) {

		insertSize--;
		trans(position);
	}

	private void trans(int position){
		for(int i=position ;i<t.length-1;i++){
			t[i]=t[i+1];
		}
		t[t.length-1]=null;
	}


	public static void main(String args[]){
		ArrayList<String> mArrayList = new ArrayList<>(6);
		mArrayList.insert("good");
		mArrayList.insert("good");
		mArrayList.insert("good");
		mArrayList.insert("good");
		mArrayList.insert("good");
		mArrayList.insert("good");
		mArrayList.insert("good");
		System.out.println(mArrayList.toString());

		mArrayList.remove(5);

		System.out.println(mArrayList.toString());
	}

	private void rangeCheckForAdd(int index) {
		if (index >= capacity || index < 0)
			throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
	}
	private String outOfBoundsMsg(int index) {
		return "Index: "+index+", Size: "+capacity;
	}
	@Override
	public String toString() {
		return Arrays.toString(t);
	}
}
